package com.neusoft.chanceserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.neusoft.chanceserver.dto.RespBean;
import com.neusoft.chanceserver.dto.SubchanceAdd;
import com.neusoft.chanceserver.entity.ChanceSubchance;
import com.neusoft.chanceserver.entity.Subchance;
import com.neusoft.chanceserver.mapper.ChanceSubchanceMapper;
import com.neusoft.chanceserver.mapper.SubchanceMapper;
import com.neusoft.chanceserver.service.ISubchanceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 子机会表 服务实现类
 * </p>
 *
 * @author liuxp
 * @since 2021-07-12
 */
@Service
public class SubchanceServiceImpl extends ServiceImpl<SubchanceMapper, Subchance> implements ISubchanceService {

    @Autowired
    SubchanceMapper subchanceMapper;

    @Autowired
    ChanceSubchanceMapper chanceSubchanceMapper;

    @Override
    public RespBean searchSubchanceByChanceId(Integer chanceId) {
        List<Integer> subchanceIds = new ArrayList<>();
        LambdaQueryWrapper<ChanceSubchance> chanceSubchanceLambdaQueryWrapper = Wrappers.lambdaQuery();
        chanceSubchanceLambdaQueryWrapper.eq(ChanceSubchance::getChanceId, chanceId);
        List<ChanceSubchance> chanceSubchances = chanceSubchanceMapper.selectList(chanceSubchanceLambdaQueryWrapper);
        for(ChanceSubchance chanceSubchance : chanceSubchances){
            subchanceIds.add(chanceSubchance.getSubchanceId());
        }
        List<Subchance> subchances = new ArrayList<>();
        if(subchanceIds.size() > 0)
            subchances = subchanceMapper.selectBatchIds(subchanceIds);
        RespBean respBean = RespBean.ok(200, "Find " + subchances.size() + " subchances!");
        respBean.setObj(subchances);
        return respBean;
    }

    @Override
    public RespBean deleteSubchance(Integer subchanceId) {
        RespBean respBean = RespBean.ok(200, "Subchance deleted successfully!");
        LambdaQueryWrapper<ChanceSubchance> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ChanceSubchance::getSubchanceId, subchanceId);
        chanceSubchanceMapper.delete(wrapper);
        subchanceMapper.deleteById(subchanceId);
        return respBean;
    }

    @Override
    public RespBean updateSubchance(Subchance subchance) {
        RespBean respBean = RespBean.ok(200, "Subchance updated successfully!");
        subchanceMapper.updateById(subchance);
        return respBean;
    }

    @Override
    public RespBean addSubchance(SubchanceAdd subchanceAdd) {
        RespBean respBean = RespBean.ok(200, "Subchance added successfully!");
        subchanceMapper.insert(subchanceAdd.getSubchance());
        int subchanceid = subchanceAdd.getSubchance().getSubchanceId();
        ChanceSubchance chanceSubchance = new ChanceSubchance();
        chanceSubchance.setChanceId(subchanceAdd.getChanceId());
        chanceSubchance.setSubchanceId(subchanceid);
        chanceSubchanceMapper.insert(chanceSubchance);
        return respBean;
    }


}
